//用户管理的路由

//引入express框架
const express = require("express");
// 引入数据库连接
const db = require("../modules/DBhelper");
//引入时间模块
const moment = require("moment");


//创建路由
let router = express.Router();



router.post("/login", async function (request, response, next) {
    //得到前端传过来的数据
    let userobj = request.body;
    let sql = "SELECT*FROM `userinfo` WHERE `u_phone`=? AND `u_pwd`=?;";
    let params = [
        userobj.phone,
        userobj.upwd
    ];
    let result = await db.exec(sql, params);
    let islogin = result && result.length >= 1;
    if (islogin) {
        delete (result[0].u_pwd);
    }
    let message = {
        msg: islogin ? "登录成功" : "登录失败",
        data: islogin ? result : '',
        status: islogin ? 200 : -200
    }
    response.json(message);
})


router.post("/reg", async function (request, response, next) {
   //得到前端传过来的数据
   let userobj = request.body;

   let sql = "INSERT INTO `userinfo` (`u_nickname`,`u_pwd`,`u_phone`)VALUE(?,?,?);";
   let params = [
       userobj.nickname,
       userobj.upwd,
       userobj.phone,
       moment().format("YYYY-MM-DD HH:mm:ss")
   ];
   let result = await db.exec(sql, params);
   let isreg = result && result.affectedRows >= 1;
   let message = {
       msg: isreg ? "注册成功" : "注册失败",
       status: isreg ? 200 : -200
   }
   response.json(message);

})

//暴露
module.exports = {
    router,
}